package com.itboye.textileexpes.adapter;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.graphics.Paint;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.itboye.tbshopcar.listener.OnShoppingCartChangeListener;
import com.itboye.tbshopcar.listener.ShoppingCartBiz;
import com.itboye.tbshopcar.listener.ToastHelper;
import com.itboye.tbshopcar.listener.UIAlertView;
import com.itboye.textileexpes.beans.ShopCartModel;
import com.itboye.textileexpres.R;
import com.itboye.textileexpres.base.ShoppingCartBean;

public class MyExpandableListAdapter extends BaseExpandableListAdapter {
	private Context mContext;
	// private List<ShoppingCartBean> mListGoods = new
	// ArrayList<ShoppingCartBean>();
	private OnShoppingCartChangeListener mChangeListener;
	private boolean isSelectAll = false;
	private List<ShopCartModel> goodsList = new ArrayList<ShopCartModel>();
	OnClickListener deletelistener;

	// private List<ShopCartModel> goodsList = new ArrayList<ShopCartModel>();

	public MyExpandableListAdapter(Context context,
			OnClickListener deletelistener) {
		mContext = context;
		this.deletelistener = deletelistener;
	}

	public void setList(List<ShopCartModel> goodsList) {
		this.goodsList = goodsList;
		setSettleInfo();
	}

	public void setOnShoppingCartChangeListener(
			OnShoppingCartChangeListener changeListener) {
		this.mChangeListener = changeListener;
	}

	public View.OnClickListener getAdapterListener() {
		return listener;
	}

	public int getGroupCount() {
		return 1;
	}

	public int getChildrenCount(int groupPosition) {
		return goodsList.size();
	}

	public Object getGroup(int groupPosition) {
		return goodsList.get(groupPosition);
	}

	public Object getChild(int groupPosition, int childPosition) {
		return goodsList.get(childPosition);
	}

	public long getGroupId(int groupPosition) {
		return groupPosition;
	}

	public long getChildId(int groupPosition, int childPosition) {
		return childPosition;
	}

	public boolean hasStableIds() {
		return false;
	}

	public View getGroupView(int groupPosition, boolean isExpanded,
			View convertView, ViewGroup parent) {
		GroupViewHolder holder = null;
		if (convertView == null) {
			holder = new GroupViewHolder();
			convertView = LayoutInflater.from(mContext).inflate(
					R.layout.item_elv_group_test, parent, false);
		}

		return convertView;
	}

	/**
	 * child view
	 */

	public View getChildView(int groupPosition, int childPosition,
			boolean isLastChild, View convertView, ViewGroup parent) {
		ChildViewHolder holder = null;
		if (convertView == null) {
			holder = new ChildViewHolder();
			convertView = LayoutInflater.from(mContext).inflate(
					R.layout.item_elv_child_test, parent, false);
			holder.tvChild = (TextView) convertView
					.findViewById(R.id.tvItemChild);
			holder.tvDel = (TextView) convertView.findViewById(R.id.tvDel);
			holder.ivCheckGood = (ImageView) convertView
					.findViewById(R.id.ivCheckGood);
			holder.rlEditStatus = (RelativeLayout) convertView
					.findViewById(R.id.rlEditStatus);
			holder.llGoodInfo = (LinearLayout) convertView
					.findViewById(R.id.llGoodInfo);
			holder.ivAdd = (TextView) convertView.findViewById(R.id.ivAdd);
			holder.ivReduce = (TextView) convertView
					.findViewById(R.id.ivReduce);
			holder.tvGoodsParam = (TextView) convertView
					.findViewById(R.id.tvGoodsParam);
			holder.tvPriceNew = (TextView) convertView
					.findViewById(R.id.tvPriceNew);
			holder.tvPriceOld = (TextView) convertView
					.findViewById(R.id.tvPriceOld);
			holder.tvPriceOld.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG);// 数字被划掉效果
			holder.tvNum = (TextView) convertView.findViewById(R.id.tvNum);
			holder.tvNum2 = (TextView) convertView.findViewById(R.id.tvNum2);
			convertView.setTag(holder);
			holder.tvShopNameGroup = (TextView) convertView
					.findViewById(R.id.tvtextren);
			holder.tvEdit = (TextView) convertView.findViewById(R.id.tvEdit);
			holder.tvtexaddrre = (TextView) convertView
					.findViewById(R.id.tvtexaddrre);

			convertView.setTag(holder);

		} else {
			holder = (ChildViewHolder) convertView.getTag();
		}
		ShopCartModel goods = goodsList.get(childPosition);
		boolean isChildSelected = goodsList.get(childPosition).isSelected();
		holder.tvShopNameGroup.setText(goodsList.get(groupPosition)
				.getContact_name());
		holder.tvtexaddrre.setText(goodsList.get(groupPosition)
				.getContact_name());
		boolean isEditing = goods.isEditing();
		String priceNew = "¥" + goods.getPrice();
		String priceOld = "¥" + goods.getOri_price();
		String num = goods.getCount();
		String pdtDesc = goods.getSku_desc();
		String goodName = goodsList.get(childPosition).getName();
//		boolean isEditing = goodsList.get(childPosition).isEditing();
//		if (isEditing) {
//			holder.tvEdit.setBackgroundResource(R.drawable.colloct_save);
//		} else {
//			holder.tvEdit.setBackgroundResource(R.drawable.collect_edit);
//		}
		holder.tvEdit.setTag(groupPosition);
		holder.tvEdit.setOnClickListener(listener);
		holder.tvShopNameGroup.setOnClickListener(listener);
		holder.ivCheckGood.setTag(groupPosition + "," + childPosition);
		holder.tvChild.setText(goodName);
		holder.tvPriceNew.setText(priceNew);
		holder.tvPriceOld.setText(priceOld);
		holder.tvNum.setText("X " + num);
		holder.tvNum2.setText(num);
		holder.tvGoodsParam.setText(pdtDesc);

		holder.ivAdd.setTag(goods);
		holder.ivReduce.setTag(goods);
		// holder.tvDel.setTag(groupPosition + "," + childPosition);
		holder.tvDel.setTag(childPosition);

		ShoppingCartBiz.checkItem(isChildSelected, holder.ivCheckGood);
		if (isEditing) {
			holder.tvEdit.setBackgroundResource(R.drawable.colloct_save);
			holder.llGoodInfo.setVisibility(View.GONE);
			holder.rlEditStatus.setVisibility(View.VISIBLE);
		} else {
			holder.tvEdit.setBackgroundResource(R.drawable.collect_edit);
			holder.llGoodInfo.setVisibility(View.VISIBLE);
			holder.rlEditStatus.setVisibility(View.GONE);
		}

		holder.ivCheckGood.setOnClickListener(listener);
		// holder.tvDel.setOnClickListener(listener);
		holder.tvDel.setOnClickListener(deletelistener);
		holder.ivAdd.setOnClickListener(listener);
		holder.ivReduce.setOnClickListener(listener);
		holder.llGoodInfo.setOnClickListener(listener);
		return convertView;
	}

	public boolean isChildSelectable(int i, int i1) {
		return false;
	}

	View.OnClickListener listener = new View.OnClickListener() {

		public void onClick(View v) {
			switch (v.getId()) {
			// main
			case R.id.ivSelectAll:
				isSelectAll = ShoppingCartBiz.selectAll(goodsList, isSelectAll,
						(ImageView) v);
				setSettleInfo();
				notifyDataSetChanged();
				break;
			case R.id.tvEditAll:
				break;
			case R.id.btnSettle:
				// if (ShoppingCartBiz.hasSelectedGoods(mListGoods)) {
				// ToastHelper.getInstance()._toast("结算跳转");
				// } else {
				// ToastHelper.getInstance()._toast("亲，先选择商品！");
				// }
				// group
				break;
			case R.id.tvEdit:// 切换界面，属于特殊处理，假如没打算切换界面，则不需要这块代码
				int groupPosition2 = Integer
						.parseInt(String.valueOf(v.getTag()));
				boolean isEditing = !(goodsList.get(groupPosition2).isEditing());
				goodsList.get(groupPosition2).setEditing(isEditing);
				for (int i = 0; i < goodsList.size(); i++) {
					goodsList.get(i).setEditing(isEditing);
				}
				notifyDataSetChanged();
				break;
			case R.id.ivCheckGroup:
				int groupPosition3 = Integer
						.parseInt(String.valueOf(v.getTag()));
				isSelectAll = ShoppingCartBiz.selectGroup(goodsList,
						groupPosition3);
				selectAll();
				setSettleInfo();
				notifyDataSetChanged();
				break;
			// child
			case R.id.ivCheckGood:
				String tag = String.valueOf(v.getTag());
				if (tag.contains(",")) {
					String s[] = tag.split(",");
					int groupPosition = Integer.parseInt(s[0]);
					int childPosition = Integer.parseInt(s[1]);
					isSelectAll = ShoppingCartBiz.selectOne(goodsList,
							groupPosition, childPosition);
					selectAll();
					setSettleInfo();
					notifyDataSetChanged();
				}
				break;
			case R.id.tvDel:
				String tagPos = String.valueOf(v.getTag());
				if (tagPos.contains(",")) {
					String s[] = tagPos.split(",");
					int groupPosition = Integer.parseInt(s[0]);
					int childPosition = Integer.parseInt(s[1]);
					showDelDialog(groupPosition, childPosition);
				}
				break;
			case R.id.ivAdd:
				ShoppingCartBiz.addOrReduceGoodsNum(true, (ShopCartModel) v
						.getTag(), ((TextView) (((View) (v.getParent()))
						.findViewById(R.id.tvNum2))));
				setSettleInfo();
				break;
			case R.id.ivReduce:
				ShoppingCartBiz.addOrReduceGoodsNum(false, (ShopCartModel) v
						.getTag(), ((TextView) (((View) (v.getParent()))
						.findViewById(R.id.tvNum2))));
				setSettleInfo();
				break;
			case R.id.llGoodInfo:
				ToastHelper.getInstance()._toast("商品详情，暂未实现");
				break;
			case R.id.tvShopNameGroup:
				ToastHelper.getInstance()._toast("商铺详情，暂未实现");
				break;
			}
		}
	};

	private void selectAll() {
		if (mChangeListener != null) {
			mChangeListener.onSelectItem(isSelectAll);
		}
	}

	private void setSettleInfo() {
		String[] infos = ShoppingCartBiz.getShoppingCount(goodsList);
		// 删除或者选择商品之后，需要通知结算按钮，更新自己的数据；
		if (mChangeListener != null && infos != null) {
			mChangeListener.onDataChange(infos[0], infos[1]);
		}
	}

	private void showDelDialog(final int groupPosition, final int childPosition) {
		final UIAlertView delDialog = new UIAlertView(mContext, "温馨提示",
				"确认删除该商品吗?", "取消", "确定");
		delDialog.show();

		delDialog.setClicklistener(new UIAlertView.ClickListenerInterface() {

			public void doLeft() {
				delDialog.dismiss();
			}

			public void doRight() {
				String productID = goodsList.get(childPosition).getP_id();
				ShoppingCartBiz.delGood(productID);
				delGoods(groupPosition, childPosition);
				setSettleInfo();
				notifyDataSetChanged();
				delDialog.dismiss();
			}
		});
	}

	private void delGoods(int groupPosition, int childPosition) {
		// mListGoods.get(groupPosition).getGoods().remove(childPosition);
		// if (mListGoods.get(groupPosition).getGoods().size() == 0) {
		// mListGoods.remove(groupPosition);
		// }
		notifyDataSetChanged();
	}

	class GroupViewHolder {

		ImageView ivCheckGroup;
	}

	class ChildViewHolder {
		TextView tvShopNameGroup;
		TextView tvEdit, tvtexaddrre;
		/** 商品名称 */
		TextView tvChild;
		/** 商品规格 */
		TextView tvGoodsParam;
		/** 选中 */
		ImageView ivCheckGood;
		/** 非编辑状态 */
		LinearLayout llGoodInfo;
		/** 编辑状态 */
		RelativeLayout rlEditStatus;
		/** +1 */
		TextView ivAdd;
		/** -1 */
		TextView ivReduce;
		/** 删除 */
		TextView tvDel;
		/** 新价格 */
		TextView tvPriceNew;
		/** 旧价格 */
		TextView tvPriceOld;
		/** 商品状态的数量 */
		TextView tvNum;
		/** 编辑状态的数量 */
		TextView tvNum2;
	}
}
